package com.cxka.common.result;

import com.cxka.common.util.DateUtils;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * 统一返回对象
 */
@AllArgsConstructor
@Data
public class IResult<T> implements Serializable {

    private static final long serialVersionUID = -312980979747867182L;

    public static final int SUCCESS_STATUS = 200;
    public static final String SUCCESS_MESSAGE = "成功";
    public static final int ERROR_STATUS = 500;
    public static final String ERROR_MESSAGE = "失败";

    private String message;
    private int status;
    private Object data;
    private String timestamp = DateUtils.format(new Date(), DateUtils.DATE_TIME);

    public IResult() {

    }

    public IResult(String message) {
        this.message = message;
    }

    public IResult(int status) {
        this.status = status;
    }

    public static <T> IResult<T> success() {
        IResult<T> result = new IResult<T>();
        result.setStatus(SUCCESS_STATUS);
        result.setMessage(SUCCESS_MESSAGE);
        return result;
    }

    public static <T> IResult<T> success(Object data) {
        IResult<T> result = new IResult<T>();
        result.setData(data);
        result.setStatus(SUCCESS_STATUS);
        result.setMessage(SUCCESS_MESSAGE);
        return result;
    }

    public static <T> IResult<T> success(Object data, String message) {
        IResult<T> result = new IResult<T>();
        result.setData(data);
        result.setStatus(SUCCESS_STATUS);
        result.setMessage(message);
        return result;
    }

    public static <T> IResult<T> success(Object data, int status, String message) {
        IResult<T> result = new IResult<T>();
        result.setData(data);
        result.setStatus(status);
        result.setMessage(message);
        return result;
    }

    public static <T> IResult<T> error() {
        IResult<T> result = new IResult<T>();
        result.setMessage(ERROR_MESSAGE);
        result.setStatus(ERROR_STATUS);
        return result;
    }

    public static <T> IResult<T> error(Object data) {
        IResult<T> result = new IResult<T>();
        result.setData(data);
        result.setMessage(ERROR_MESSAGE);
        result.setStatus(ERROR_STATUS);
        return result;
    }

    public static <T> IResult<T> error(Object data, String message) {
        IResult<T> result = new IResult<T>();
        result.setData(data);
        result.setStatus(ERROR_STATUS);
        result.setMessage(message);
        return result;
    }

    public static <T> IResult<T> error(Object data, int status, String message) {
        IResult<T> result = new IResult<T>();
        result.setData(data);
        result.setStatus(status);
        result.setMessage(message);
        return result;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }


    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    @Override
    public String toString() {
        return "IResult<T>{" +
                "message='" + message + '\'' +
                ", status=" + status +
                ", data=" + data +
                ", timestamp=" + timestamp +
                '}';
    }
}